<?
/* PartyController controller class.
 * This class contains the handling code, divided into action methods.
 *
 */ 
/* @author Udo Schroeter <udo.schroeter@gmail.com>
 * @license dual license: LGPL / commercial use license
 */ 

class PartyController extends AGController
{
  function __construct($ctrName) 
  {
    parent::__construct($ctrName);
    $this->invokeModel();
    $this->model->user = &$this->user;
    $this->model->character = &$this->character;
  }

  function Index()
  {
    #$this->model->loadParty($this->user->account['uid']);
    if (isset($_POST["ids"]))
    {
      $this->model->addInvitedPeople($_POST["ids"], $this->user);
      $this->redirect('sent', 'party');
    }
    if (isset($_REQUEST['leaveteam']))
    {
      $this->model->RemoveFromGroup($_REQUEST['leaveteam']);
    }
    else if (isset($_REQUEST['jointeam']))
    {
      $this->model->AddToGroup($_REQUEST['jointeam']);
    }
    $this->partyMembers = $this->model->loadParty($this->user->localRecord['u_key'], 'N');
    $this->groupMembers = $this->model->loadParty($this->user->localRecord['u_key'], 'Y');
  }
  
  function Invite()
  {
    #$this->model->loadFBFriends($this->user->account['uid']);
    global $config;
    $config['site.pagetemplate'] = 'xfbml';
    $this->invDS = array(
      'inv_user' => $this->user->localRecord['u_key'],
      'inv_code' => md5($this->user->localRecord['u_key'].time()),
      );
    $this->inv_code = $this->invDS['inv_code'];
    $_SESSION['last_inv_code'] = $this->inv_code;
    $this->invDS['inv_key'] = DB_UpdateDataset('invitations', $this->invDS);
  }
  
  function join()
  {
    $joinUser = $_REQUEST['party'];
    $this->model->joinParty($this->user, $joinUser);
    $this->redirect('index', 'party');
  }
  
  function ignore()
  {
    $joinUser = $_REQUEST['party'];
    $this->model->clearInvitesFor($this->user->localRecord['u_key'], $joinUser);
    $this->redirect('index', 'default');
  }
  
  function sent()
  {
    $this->model->syncWithFB($this->user->localRecord['u_fb_uid'], true);
    $this->partyMembers = DB_GetList('SELECT * FROM '.getTableName('invited_people').' 
      LEFT JOIN '.getTableName('users').' ON (u_fb_uid = ip_invited_fb_uid)
      WHERE ip_inviter = "'.$this->user->localRecord['u_key'].'" 
      ORDER BY ip_date DESC
      LIMIT 500');
  }
  
  function ajax_viewparty()
  {
    global $config;
    $config['site.pagetemplate'] = 'blank';
    $this->groupMembers = $this->model->loadGroup($this->user->localRecord['u_key']);
  }  
  
  // obsolete
  function Preview()
  {
    die();
    foreach($_REQUEST as $k => $v)
    {
      if (CutSegment('_', $k) == 'inv' && $v == 'Y')
      {
        $this->model->invList[$k] = DB_GetDataset('users', $k);
      }
    }
  }
  
  // obsolete
  function send()
  {
    #$this->model->sendInvitations($_SESSION['invite'], $_SESSION['invtext']);
  }
  
  // obsolete
   function SendStandardInvitation($to, $bNewStyle = false)
   {
     die();  
     global $config;
     $typeword = $config['site.name'];
     $content  = $config['site.name'].' Invitation';
     $content .= '<fb:req-choice url=\''.$config['fb']['url'].'\' 
       label=\'Hi there,
    
I would like to invite you to join my adventuring party in '.$config['site.name'].'! You will have tons of fun! :-)

'.$this->user->localRecord['u_fb_name'].'\'/>';

     $actionText = 'Invite your friends';
       
     $bOK = SendNewRequest($to, $typeword, $content, $actionText);
     return $bOK;
   
   }
  
}


?>
